//
//  LSFActivity.swift
//  hoonpay
//
//  Created by 李胜锋 on 2017/8/7.
//  Copyright © 2017年 lishengfeng. All rights reserved.
//

import NVActivityIndicatorView
import Async

public func showMessage(_ message:String?, duration:Double = 0.25) {
    LSFActivity.share.showMessage(message, duration: duration)
}

public func startAnimating() {
    Async.main {
        LSFActivity.share.startAnimating()
    }
}

public func stopAnimating() {
    
    Async.main {
        LSFActivity.share.stopAnimating()
    }
}

public func showAnimating(_ duration:TimeInterval = 2.5) {
    startAnimating()
    
    Async.main(after: duration) {
        stopAnimating()
    }
}

class LSFActivity:NVActivityIndicatorViewable {
    
    public static let share = LSFActivity()
    
    init(type:NVActivityIndicatorType = .ballSpinFadeLoader,
         color:UIColor = UIColor.white,
         backgroundColor:UIColor = UIColor.clear) {
        
        //设置默认提示框
        NVActivityIndicatorView.DEFAULT_TYPE = type
        NVActivityIndicatorView.DEFAULT_COLOR = color
        NVActivityIndicatorView.DEFAULT_BLOCKER_BACKGROUND_COLOR = backgroundColor
    }
    
    init() {
        
        //设置默认提示框
        NVActivityIndicatorView.DEFAULT_TYPE = .ballPulse
        NVActivityIndicatorView.DEFAULT_COLOR = Config.default.themeColor
        NVActivityIndicatorView.DEFAULT_BLOCKER_BACKGROUND_COLOR = UIColor.clear
    }

    /**
     Display UI blocker.
     
     Appropriate NVActivityIndicatorView.DEFAULT_* values are used for omitted params.
     
     - parameter size:                 size of activity indicator view.
     - parameter message:              message displayed under activity indicator view.
     - parameter messageFont:          font of message displayed under activity indicator view.
     - parameter type:                 animation type.
     - parameter color:                color of activity indicator view.
     - parameter padding:              padding of activity indicator view.
     - parameter displayTimeThreshold: display time threshold to actually display UI blocker.
     - parameter minimumDisplayTime:   minimum display time of UI blocker.
     */
    public func startAnimating(
        _ size: CGSize? = nil,
        message: String? = nil,
        messageFont: UIFont? = nil,
        type: NVActivityIndicatorType? = nil,
        color: UIColor? = nil,
        padding: CGFloat? = nil,
        displayTimeThreshold: Int? = nil,
        minimumDisplayTime: Int? = nil,
        backgroundColor: UIColor? = nil,
        textColor: UIColor? = nil) {
        let activityData = ActivityData(size: size,
                                        message: message,
                                        messageFont: messageFont,
                                        type: type,
                                        color: color,
                                        padding: padding,
                                        displayTimeThreshold: displayTimeThreshold,
                                        minimumDisplayTime: minimumDisplayTime,
                                        backgroundColor: backgroundColor,
                                        textColor: textColor)
        
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
    }
    
    /**
     Remove UI blocker.
     */
    public func stopAnimating() {
        NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
    }

    ///显示提示语
    public func  showMessage(_ message:String?, duration:Double = 2.5) {
        Async.main {
            self.startAnimating(message: message,
                           type: NVActivityIndicatorType.blank,
                           color: Config.default.themeColor)
        }
        
        Async.main(after: duration) { [weak self] in
            self?.stopAnimating()
        }
    }
}
